from stduent import Student
class StudentManger :
    def __init__(self) :
        self.students_dict = {}
        self.menuitem={
            "1":self.add_student,
            "2":self.show_students,
            "3":self.get_students,
            "4":self.update_student,
            "5":self.delete_student,
            "s":self.save_to_file,
            "l":self.load_from_file
        }
        return

    def save_to_file(self) :
        students_list = self.students_dict.values()
        with open("students.txt", "w") as f:
            for student in students_list :
                f.write(str(student) + "\n")
        return

    def load_from_file(self) :
        self.students_dict.clear()
        with open("students.txt", "r",encoding='UTF-8') as file :
            lines = file.readlines()
            for line in lines :
                student_info = line[:-1].split(',')
                id = student_info[0]
                name = student_info[1]
                score = student_info[2]
                student = Student(id, name, score)
                self.students_dict[id]=student
        return

    def get_students(self) :
        inputString = input("请输入学号:")
        if inputString in self.students_dict.keys():
            print(self.students_dict[inputString])
        else:
            print("没有相关的信息")
        inputString = input("任意键继续....")
        return

    def update_student(self) :
        id = input("请输入学号:")
        if id in self.students_dict.keys():
            print("原信息%s:"%self.students_dict[id])
            inputString=input("请输入新的信息(格式：姓名，分数)")
            info_list = inputString.split(',')
            student = self.students_dict[id]
            student.name = info_list[0]
            student.score = info_list[1]
            self.students_dict[id] = student
        else:
            print("没有相关信息")
        inputString = input("任意键继续....")
        return

    def delete_student(self) :
        inputString = input("请输入学号:")
        if inputString in self.students_dict.keys():
            confirm = input("是否删除[%s](y/n)?"%self.students_dict[inputString])
            if confirm == 'y' or confirm == "Y":
                del self.students_dict[inputString]
        else:
            print("没有相关信息")
        inputString = input("任意键继续....")
        return

    def show_students(self):
        for student in self.students_dict.keys():
            print(self.students_dict[student])
        inputString = input("任意键继续....")
        return

    def add_student(self):
        inputstring = input("请输入学生信息（格式：学号,姓名,分数)")
        infolist = inputstring.split(',')
        id = infolist[0]
        if id not in self.students_dict.keys():
            name = infolist[1]
            score = infolist[2]
            self.students_dict[id] = Student(id, name,score)
        else:
            print("%s信息已经存在")
        inputString = input("任意键继续....")
        return

    @staticmethod
    def show_menu():
        """
        显示菜单
        ******************************
        欢迎使⽤【学⽣管理系统】 V1.0
        1.添加学⽣
        2.显示全部
        3.查询学⽣
        4.修改学⽣
        5.删除学⽣
        0.退出系统
        ******************************
        """
        print("*" * 30)
        print("欢迎使⽤【学⽣管理系统】 V1.0")
        print("1.添加学⽣")
        print("2.显示全部")
        print("3.查询学⽣")
        print("4.修改学⽣")
        print("5.删除学⽣")
        print("s.保存数据")
        print("l.读入数据")
        print("0.退出系统")
        print("*" * 30)
        return

    def startup(self):
        self.load_from_file()
        while True:
            self.show_menu()
            choice = input("请选择操作：")
            if choice == "0":
                exit(0)
            else:
                try:
                    if choice in self.menuitem.keys():
                        (self.menuitem[choice])()
                    else:
                        print("无此功能...")
                except:
                    print("系统出了问题...")